package net.qiqb.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import net.qiqb.core.domain.event.DomainEventPublisher;
import net.qiqb.core.domain.event.DomainEvent;
import net.qiqb.spring.ApplicationContextHolder;
import org.springframework.amqp.core.AmqpTemplate;

@Slf4j
public class RabbitmqDomainEventPublisher implements DomainEventPublisher {


    private AmqpTemplate rabbitTemplate;

    @Override
    public void publishEvent(DomainEvent event) {
        if (rabbitTemplate == null) {
            this.rabbitTemplate = ApplicationContextHolder.get().getBean(AmqpTemplate.class);
        }
        rabbitTemplate.convertAndSend("domain_event_exchange_name", "", event);
    }
}
